package com.jzl.swing.ui;

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPasswordField;

import com.jzl.swing.entity.User;
import com.jzl.swing.service.UserService;
import com.jzl.swing.service.impl.UserServiceImpl;

import javax.swing.JComboBox;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class ModifyUserUI {

	private JFrame frame;
	private JPasswordField passwordOld;
	private JPasswordField passwordReNew;
	private JPasswordField passwordNew;
	private UserService userService = new UserServiceImpl();
	User user;

	
	
	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					ModifyUserUI window = new ModifyUserUI(args[0]);
					window.frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the application.
	 */
	public ModifyUserUI() {
		initialize();
	}
	
	public ModifyUserUI(String user_name){
		user = new User();
		user.setUser_name(user_name);
		try {
			user = userService.queryUser(user);
			System.out.println(user.getUser_name());
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		initialize();
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		frame = new JFrame();
		frame.setBounds(100, 100, 450, 483);
		frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		frame.getContentPane().setLayout(null);
		
		JLabel label = new JLabel("用户名：");
		label.setBounds(81, 43, 61, 16);
		frame.getContentPane().add(label);
		
		JLabel lblUserName = new JLabel("New label");
		lblUserName.setBounds(154, 43, 61, 16);
		lblUserName.setText(user.getUser_name());
		frame.getContentPane().add(lblUserName);
		
		JLabel label_1 = new JLabel("旧密码：");
		label_1.setBounds(81, 94, 61, 16);
		frame.getContentPane().add(label_1);
		
		passwordOld = new JPasswordField();
		passwordOld.setBounds(154, 89, 121, 26);
		frame.getContentPane().add(passwordOld);
		
		JLabel label_2 = new JLabel("新密码：");
		label_2.setBounds(81, 154, 61, 16);
		frame.getContentPane().add(label_2);
		
		JLabel label_3 = new JLabel("确认新密码：");
		label_3.setBounds(81, 225, 89, 16);
		frame.getContentPane().add(label_3);
		
		passwordReNew = new JPasswordField();
		passwordReNew.setBounds(154, 220, 130, 26);
		frame.getContentPane().add(passwordReNew);
		
		passwordNew = new JPasswordField();
		passwordNew.setBounds(154, 149, 121, 26);
		frame.getContentPane().add(passwordNew);
		
		JLabel label_4 = new JLabel("用户权限：");
		label_4.setBounds(81, 300, 89, 16);
		frame.getContentPane().add(label_4);
		
		JComboBox comboBox = new JComboBox();
		comboBox.setModel(new DefaultComboBoxModel(new String[] {"普通员工", "管理员"}));
		comboBox.setBounds(154, 296, 108, 27);
		frame.getContentPane().add(comboBox);
		
		JButton button = new JButton("修改");
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String oldPass = String.valueOf(passwordOld.getPassword());
				String newPass = String.valueOf(passwordNew.getPassword());
				String renewPass = String.valueOf(passwordReNew.getPassword());
				if ("".equals(oldPass) || "".equals(newPass) || "".equals(renewPass)) {
					JOptionPane.showConfirmDialog(frame, "密码不能为空");
					return;
				}
				if (!renewPass.equals(newPass)) {
					JOptionPane.showMessageDialog(frame, "新密码不一致");
					return;
				}
				if (!user.getUser_pass().equals(oldPass)) {
					JOptionPane.showMessageDialog(frame, "密码错误");
					return;
				}
				user.setUser_pass(newPass);
				try {
					int i = userService.modifyUser(user);
					if (i>0) {
						JOptionPane.showMessageDialog(frame, "修改成功！");
						frame.dispose();
					}
				} catch (Exception e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
				
			}
		});
		button.setBounds(57, 385, 117, 29);
		frame.getContentPane().add(button);
		
		JButton button_1 = new JButton("关闭");
		button_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				int i = JOptionPane.showConfirmDialog(frame, "您填写的信息将不会被保存，是否退出？");
				if (i==0) {
					frame.dispose();
				}
			}
		});
		button_1.setBounds(239, 385, 117, 29);
		frame.getContentPane().add(button_1);
	}
}
